package gewu.suleju.controller;

import gewu.suleju.model.pojo.HouseInfor;
import gewu.suleju.model.pojo.HousePic;
import gewu.suleju.model.pojo.RentEntrustInfo;

import gewu.suleju.model.vo.Result;
import gewu.suleju.sevice.imp.HouseInforServiceImp;
import gewu.suleju.sevice.imp.HousePicServiceImp;
import gewu.suleju.sevice.imp.RentEntrustInfoServiceImp;
import gewu.suleju.util.JWTUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("EntrustInfo")
public class EntrustInfoController {
    @Autowired
    private RentEntrustInfoServiceImp rentEntrustInfoServiceImp;
    @Autowired
    private HouseInforServiceImp houseInforServiceImp;
    @Autowired
    private HousePicServiceImp housePicServiceImp;
    @ResponseBody
    @GetMapping("EntrustInfoByUserId")


    public Result EntrustInfoByUserId(HttpServletRequest request, HttpServletResponse response){
        String token = request.getHeader("token");
        Claims claims = JWTUtil.checkJWT(token);
        Integer userid=(Integer)claims.get("id");
        List<RentEntrustInfo> rentEntrustInfoList=rentEntrustInfoServiceImp.findRentEntrustInfosByUserId(userid);
        List<HouseInfor>  houseInforList=new ArrayList();
        Map<Object,Object> picmap=new HashMap<Object,Object>();
        Map<Object,Object> map=new HashMap<Object,Object>();
        for(int i=0;i<rentEntrustInfoList.size();i++){
          houseInforList.add(i,houseInforServiceImp.findHouseInforById(  rentEntrustInfoList.get(i).getHouseInforId()));

          List<HousePic>   housePicList=housePicServiceImp.findHousePicsByHouseInforId(rentEntrustInfoList.get(i).getHouseInforId());

          picmap.put(i,housePicList);
        }
        map.put("HouseInfor",houseInforList);
        map.put("房子图片",picmap);
        map.put("房子委托",rentEntrustInfoList);

        return new Result("200","成功",map);
    }
}
